<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="padding: 10px 0">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="show = true"
              >
                新增角色
              </el-button>
            </el-row>
            <!-- 表格 -->
            <el-table :data="list">
              <!-- el-table-column 代表一列 所有列加起来代表一行的数据-->
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" prop="name" width="240" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <!-- <span>{{ row }}</span> -->
                  <el-button
                    size="small"
                    type="success"
                    @click="openAssingPerm(row)"
                  >
                    分配权限
                  </el-button>
                  <el-button size="small" type="primary">编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delRole(row.id)"
                  >
                    删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                :page-size="query.pagesize"
                :current-page="query.page"
                :total="total"
                layout="prev,pager,next"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司设置">
            <h1>123</h1>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 新增角色  -->
    <el-dialog title="新增角色" :visible.sync="show" width="40%" @close="close">
      <!-- 表单 -->
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="100px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="show = false">取消</el-button>
        <el-button type="primary" @click="addRole">确定</el-button>
      </span>
    </el-dialog>
    <!-- 给角色分配权限点 -->
    <AssignPerm ref="perm" :show-assign-dialog.sync="showAssignDialog" />
  </div>
</template>

<script>
import { getRoleList, deleteRole, addRole } from '@/api/setting'
import AssignPerm from './components/assign-perm'
export default {
  components: {
    AssignPerm
  },
  data () {
    return {
      showAssignDialog: false,
      // 显示弹层
      show: false,
      // 表单数据（新增|编辑）
      roleForm: {
        name: '',
        description: ''
      },
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }],
        description: [{ required: true, message: '角色描述不能为空', trigger: 'blur' }]
      },
      list: [],
      // 查询参数
      query: {
        page: 1, // 页码
        pagesize: 2 // 每页几条数据
      },
      total: 0
    }
  },
  created () {
    this.getList()
  },
  methods: {
    // 打开角色分配权限弹层
    openAssingPerm (role) {
      console.log('当前角色', role)
      this.showAssignDialog = true
      this.$refs.perm.getRoleList(role.id)
    },
    // 新增角色
    async addRole () {
      try {
        await this.$refs.roleForm.validate()
        // 校验走到到
        await addRole(this.roleForm)
        this.query.page = 1
        this.getList()
        // 弹窗关闭
        this.show = false
        this.$message.success('新增成功')
      } catch (error) {
        console.log(error)
      }
    },
    // 删除角色
    async delRole (id) {
      try {
        await this.$confirm('确认删除吗？', '提示')
        await deleteRole(id)
        this.query.page = 1
        this.getList()
        this.$message.success('删除成功')
      } catch (error) {
        console.log('点击取消')
      }
    },
    // 切换分页
    changePage (currPage) {
      console.log(currPage)
      this.query.page = currPage
      this.getList()
    },
    // 获得角色列表
    async getList () {
      const { total, rows } = await getRoleList(this.query)
      console.log('获得的数据', rows)
      this.list = rows
      this.total = total
    },
    close () {
      console.log('弹层关闭')
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
